<template>
    <div>
        <input type="text" v-model="cs.name">——
        <select v-model="cs.sname">
            <option :value="info.sname" v-for="info in list">{{ info.sname }}</option>
        </select>
        <input type="button" @click="show" value="查找">
        <table class="table table-bordered">

            <tbody>
                <tr>
                    <td>机构名称</td>
                    <td>图片</td>
                    <td>特色服务</td>
                    <td>营业时间</td>
                    <td>地址</td>
                    <td>电话</td>
                    <td>状态</td>
                    <td>操作</td>
                </tr>
                <tr v-for="a in as.row" :key="a.id">
                    <td>{{ a.name }}</td>
                    <td><img :src="a.img" width="70" height="70"></td>
                    <td>{{ a.sname }}</td>
                    <td>{{ a.time }}</td>
                    <td>{{ a.address }}</td>
                    <td>{{ a.tel }}</td>
                    <td>{{ a.style == true ? "已发布" : "未发布" }}</td>
                    <td><button @click="up(a.id)">{{ a.style == true ? "发布" : "发布" }}</button></td>
                    <td><button @click="del(a.id)">删除</button></td>
                </tr>
            </tbody>
        </table>
    </div>
    <div>
        <span>每一页{{ cs.pagesize }}条</span>--
        <span>共{{ as.pagecount }}条</span>--
        <span>当前{{ cs.pageindex }}/{{ as.sizecount }}页</span>
        <input type="button" value="首页" @click="fy(1)">
        <input type="button" value="上一页" @click="fy(cs.pageindex - 1)">
        <input type="button" value="下一页" @click="fy(cs.pageindex + 1)">
        <input type="button" value="尾页" @click="fy(as.sizecount)">
    </div>

</template>

<script setup lang="ts">
import axios from 'axios';
import { ref, reactive, onMounted } from "vue";
import moment from 'moment';
import { useStore } from '@/stores/ioi';
import { useRouter, useRoute } from "vue-router";
const router = useRouter();
const route = useRoute();
const a = useStore();

let list: any = ref([]);
let cs: any = reactive(
    {
        pageindex: 1,
        pagesize: 2,
        sname: "",
        name: ""
    }
)
let add: any = reactive(
    {
        "name": "",
        "img": "",
        "sname": "",
        "time": "",
        "address": "",
        "tel": "",
        "style": true,
        "isdel": true
    }
)
const bing = () => {
    axios({
        url: 'https://localhost:7235/api/Emp/bind',
        method: 'get',
        params: add
    })
        .then(res => {
            console.log(res);
            list.value = res.data;
        })
        .catch(err => {
            console.log(err);

        })
}
let as: any = reactive(
    {
        pagecount: 0,
        sizecount: 0,
        row: []
    }
)
const fy = (f: any) => {
    if (f < 1) {
        cs.pageindex = 1;
        return;
    }
    if (f > as.sizecount) {
        cs.pageindex = as.sizecount;
        return;
    }
    cs.pageindex = f;
    show();
}
onMounted(() => {
    show();
    bing();
})
const show = () => {
    axios({
        url: 'https://localhost:7235/api/Emp/show',
        method: 'get',
        params: cs
    })
        .then(res => {
            console.log(res);
            as.pagecount = res.data.pagecount;
            as.sizecount = res.data.sizecount;
            as.row = res.data.row;
        })
        .catch(err => {
            console.log(err);

        })
}
const up = (id: any) => {
    let f = confirm("确认修改吗");
    if (f) {
        axios({
            url: 'https://localhost:7235/api/Emp/up',
            method: 'post',
            params: {
                id: id
            }
        })
            .then(res => {
                console.log(res);
                alert("修改成功");
                show();
            })
            .catch(err => {
                console.log(err);

            })
    }

}
const del = (id: any) => {
    let f = confirm("确认删除吗");
    if (f) {
        axios({
            url: 'https://localhost:7235/api/Emp/del',
            method: 'post',
            params: {
                id: id
            }
        })
            .then(res => {
                console.log(res);
                alert("删除成功");
                show();
            })
            .catch(err => {
                console.log(err);

            })
    }

}
</script>

<style scoped></style>